<?php
/**
 * Zend Framework application
 *
 * @category     Application
 * @package      Admin
 * @subpackage   ViewHelper
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2010 Krzysztof Kardasz
 */

/**
 * Węzeł
 *
 * @category     Application
 * @package      Admin
 * @subpackage   ViewHelper
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2010 Krzysztof Kardasz
 * @version 	 1.0
 */
class Hesperos_View_Helper_Node extends Zend_View_Helper_Abstract
{
    /**
     * Obiekt węzła
     *
     * @var null|Zend_Db_Table_Row_Abstract
     */
    protected $_row = null;

    /**
     * Okruszki
     *
     * @var array
     */
    protected $_breadcrumbs = null;

    /**
     * Zwraca instancję obiektu
     *
     * @param  Zend_Db_Table_Row_Abstract $row
     * @return Admin_View_Helper_Node
     */
    public function node (Zend_Db_Table_Row_Abstract $row)
    {
        $this->_row = $row;
        return $this;
    }

    /**
     * Dodaje do nagłówka metadane węzła (title, keywords, description)
     *
     * @return string
     */
    public function url (array $params = array())
    {
        if(!empty($this->_row->url_custom)) {
            $url = $this->_row->url_custom;
        } else {
            $url = implode('/', array($this->_row->language, $this->_row->url));
        }
        if(!empty($params)) {
            $url .= '_' . implode('_', $params);
        }
        return $this->view->baseUrl($url);
    }

    /**
     * Dodaje do nagłówka metadane węzła (title, keywords, description)
     *
     * @return string
     */
    public function appendMetaData ()
    {
        $description = Hesperos_Config_Manager::get('site', $this->_row->language)->meta_description;
        $keywords    = Hesperos_Config_Manager::get('site', $this->_row->language)->meta_keywords;

        if(!empty($this->_row->meta_description)) {
            $description = $this->_row->meta_description;
        }

        if(!empty($this->_row->meta_keywords)) {
            $keywords = $this->_row->meta_keywords;
        }

        if(!empty($this->_row->meta_title)) {
            $this->view->headTitle($this->_row->meta_title);
        } else {
            $this->view->headTitle(Hesperos_Config_Manager::get('site', $this->_row->language)->get('site_name', 'Chiesi'));
            foreach($this->breadcrumbs() as $item) {
                $this->view->headTitle($item['title']);
            }
        }

        if(!empty($description)) {
            $this->view->headMeta()->appendName('description',  $description);
        }

        if(!empty($keywords)) {
            $this->view->headMeta()->appendName('keywords',  $keywords);
        }
    }

    /**
     * Zwraca okruszki węzła
     *
     * @return array
     */
    public function breadcrumbs ()
    {
        if($this->_breadcrumbs !== null) {
            return $this->_breadcrumbs;
        }
        $table = new Application_Model_Gateway_Table_View_NodeBreadCrumbs ();

        return $this->_breadcrumbs = $table->fetchAll($table
            ->select()
            ->from($table->getTableName(), array('title','language', 'url'))
            ->where('node_id = ?', $this->_row->id)
            ->where('node_language = ?', $this->_row->language)
            ->where('node_type = ?', $this->_row->type)
        )->toArray();
    }

    /**
     * Zwraca datę utworzenia
     *
     * @param string $format format
     * @return string
     */
    public function date ($format = 'Y/m/d')
    {
        $time = strtotime($this->_row->date);
        if($time > 0) {
            return date($format, $time);
        }
        return $this->view->i18n()->_('brak');
    }

    /**
     * Zwraca datę utworzenia
     *
     * @param string $format format
     * @return string
     */
    public function created ($format = 'Y/m/d')
    {
        $time = strtotime($this->_row->created_at);
        if($time > 0) {
            return date($format, $time);
        }
        return $this->view->i18n()->_('brak');
    }

    /**
     * Zwraca datę modyfikacji
     *
     * @param string $format format
     * @return string
     */
    public function modified ($format = 'Y/m/d')
    {
        $time = strtotime($this->_row->modified_at);
        if($time > 0) {
            return date($format, $time);
        }
        return $this->view->i18n()->_('brak');
    }
}